home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS19.ADF / C / Gadgets / str.c < prev    next >
C/C++ Source or Header  |  1989-01-27  |  13KB  |  313 lines

  1.  
  2. /****************************************************************/
  3. /*  Example "C" program showing use of string gadgets.          */
  4. /*  This was compiled using Lattice "C", AmigaDos V1.2          */
  5. /*  The code depends on V1.2 functions to operate correctly.    */
  6. /*  It is intended to be run from CLI.                */
  7. /*                                 */
  8. /*  Copyright (C) 1987 H. Maybeck Tolly, TollySoft        */
  9. /* This program is in the public domain and may be distributed  */
  10. /* free of charge.                                              */
  11. /*                                                              */
  12. /****************************************************************/
  13.  
  14. #include "exec/types.h"
  15. #include "exec/exec.h"
  16. #include "intuition/intuition.h"
  17. #include "graphics/gfxbase.h"
  18.  
  19. UWORD StrVectors[] = {0, 0,  97, 0,   97, 9,   0, 9, 0,0};
  20. struct Border StrBorder = {
  21.    -1, -1,                 /* initial offsets, gadget relative */
  22.    3, 2, JAM1,             /* pens (fore, back) and drawmode */
  23.    5,                      /* number of vectors */
  24.    StrVectors,             /* pointer to the actual array of vectors */
  25.    NULL                    /* no next border */
  26. };
  27.  
  28. char UndoBuffer[5][41];
  29. char item_string[10][41] =  {{"0 String item\0                          "},
  30.                              {"1 String item\0                          "},
  31.                              {"2 String item\0                          "},
  32.                              {"3 String item\0                          "},
  33.                              {"4 String item\0                          "},
  34.                              {"5 String item\0                          "},
  35.                              {"6 String item\0                          "},
  36.                              {"7 String item\0                          "},
  37.                              {"8 String item\0                          "},
  38.                              {"9 String item\0                          "}};
  39.  
  40. struct StringInfo StringInfo[5]= {
  41. {
  42.    item_string[0], /* pointer to I/O buffer */
  43.    UndoBuffer[0],  /* pointer to undo buffer */
  44.    0,             /* buffer position */
  45.    41,            /* max number of chars, including NULL */
  46.    0, 0,          /* first char in display, undo positions */
  47.    0,             /* number of chars (currently) in the buffer */
  48.    0, 0, 0,       /* position variables calculated by Intuition */
  49.    NULL,          /* no pointer to RastPort */
  50.    0,             /* not a LongInt string gadget */
  51.    NULL           /* no pointer to alternate keymap */
  52. },
  53. {item_string[1],UndoBuffer[1],0,41,0,0,0,0,0,0,NULL,0,NULL},
  54. {item_string[2],UndoBuffer[2],0,41,0,0,0,0,0,0,NULL,0,NULL},
  55. {item_string[3],UndoBuffer[3],0,41,0,0,0,0,0,0,NULL,0,NULL},
  56. {item_string[4],UndoBuffer[4],0,41,0,0,0,0,0,0,NULL,0,NULL}
  57. };
  58.  
  59. struct Gadget StrGadget[5] = {
  60. {
  61.    NULL,             /* pointer to Next Gadget */
  62.    6,20, -509, 7,    /* (Left Top Width Height) Hit Box */
  63.    GADGHCOMP | GRELWIDTH,   /* Flags */
  64.    GADGIMMEDIATE | RELVERIFY,        /* Activation flags */
  65.    STRGADGET,        /* Type */
  66.    (APTR)&StrBorder, /* pointer to Border Image */
  67.    NULL,             /* no pointer to SelectRender */
  68.    NULL,             /* pointer to GadgetText */
  69.    0,                /* no MutualExclude */
  70.    (APTR)&StringInfo[0],      /* pointer to SpecialInfo */
  71.    0,                /* no ID */
  72.    NULL              /* no pointer to special data */
  73. },
  74.    {&StrGadget[0],6,40,96,7,GADGHCOMP ,GADGIMMEDIATE | RELVERIFY,
  75.    STRGADGET,(APTR)&StrBorder,NULL,NULL,0,(APTR)&StringInfo[1],0,NULL},
  76.  
  77.    {&StrGadget[1],6,60,96,7,GADGHCOMP  ,GADGIMMEDIATE | RELVERIFY,
  78.    STRGADGET,(APTR)&StrBorder,NULL,NULL,0,(APTR)&StringInfo[2],0,NULL},
  79.  
  80.    {&StrGadget[2],6,80,96,7,GADGHCOMP ,GADGIMMEDIATE | RELVERIFY,
  81.    STRGADGET,(APTR)&StrBorder,NULL,NULL,0,(APTR)&StringInfo[3],0,NULL},
  82.  
  83.    {&StrGadget[3],6,100,96,7,GADGHCOMP | GADGDISABLED ,
  84.    GADGIMMEDIATE | RELVERIFY, STRGADGET,(APTR)&StrBorder,NULL,
  85.    NULL,0,(APTR)&StringInfo[4],0,NULL}
  86. };
  87.  
  88. struct IntuitionBase *IntuitionBase = 0;
  89. struct GfxBase *GfxBase = 0;
  90.  
  91. struct Window *ControlWindow = NULL;
  92. struct IntuiMessage *MyIntuiMessage;
  93. struct NewWindow NewControlWindow = {
  94.    20, 20,                    /* start LeftEdge, TopEdge */
  95.    605, 172,                  /* start Width, Height */
  96.    2, 3,                      /* DetailPen, BlockPen */
  97.    GADGETUP | CLOSEWINDOW | MENUPICK,   /* IDCMP FLAGS */
  98.    WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | ACTIVATE,   /* Flags */
  99.    &StrGadget[4],             /* Pointer to FirstGadget */
  100.    NULL,                      /* no pointer to first CheckMark */
  101.    "String Gadgets",          /* Title (can be NULL) */
  102.    NULL,                      /* no Pointer to Screen */
  103.    NULL,                      /* no Pointer to BitMap */
  104.    20, 20,                    /* Min/max  Sizable to (w/h) */
  105.    321, 123,                  /* These aint used, can't size */
  106.    WBENCHSCREEN               /* Type of screen window appears in */
  107. };
  108. /*****************************************************************/
  109. /* Menu declarations                                             */
  110. /*****************************************************************/
  111.  
  112.    struct IntuiText ghost_text = {
  113.         2,1,JAM2,                      /* Frontpen, Backpen, Draw Mode   */
  114.         5,1,NULL,                      /* Left and Top offsets, Font     */
  115.         "GHOST",NULL  } ;              /* Text to display,next IntuiText */
  116.  
  117.    struct MenuItem ghost_item = {
  118.         NULL,0,5,140,10,            /* Next item, left,top,width,height */
  119.         HIGHCOMP | ITEMENABLED | ITEMTEXT | COMMSEQ,
  120.         NULL, (APTR) &ghost_text,  /* MutualExclude, ItemFill (text)    */
  121.         NULL, 'G', NULL } ;        /* SelectFill, Command, Subitem      */
  122.  
  123.    struct IntuiText scrollup_text = {
  124.         2,1,JAM2,                  /* Frontpen, Backpen, Draw Mode   */
  125.         5,1,NULL,                  /* Left and Top offsets, Font     */
  126.         "SCROLL UP",NULL  } ;      /* Text to display,next IntuiText */
  127.  
  128.    struct MenuItem scrollup_item = {
  129.         &ghost_item,0,15,140,10,      /* Next item, left,top,width,height */
  130.         HIGHCOMP | ITEMENABLED | ITEMTEXT | COMMSEQ,
  131.         NULL, (APTR) &scrollup_text,  /* MutualExclude, ItemFill (text)  */
  132.         NULL, 'U', NULL } ;        /* SelectFill, Command, Subitem       */
  133.  
  134.    struct IntuiText scrolldn_text = {
  135.         2,1,JAM2,                    /* Frontpen, Backpen, Draw Mode   */
  136.         5,1,NULL,                    /* Left and Top offsets, Font     */
  137.         "SCROLL DOWN",NULL  } ;      /* Text to display,next IntuiText */
  138.  
  139.    struct MenuItem scrolldn_item = {
  140.         &scrollup_item,0,25,140,10,  /* Next item, left,top,width,height */
  141.         HIGHCOMP | ITEMENABLED | ITEMTEXT | COMMSEQ,
  142.         NULL, (APTR) &scrolldn_text,  /* MutualExclude, ItemFill (text) */
  143.         NULL, 'D', NULL } ;        /* SelectFill, Command, Subitem      */
  144.  
  145.    struct Menu gadget_menu = {
  146.         NULL, 0, 0, 60, 10,        /* Next menu, left,top,width,height */
  147.         MENUENABLED, "Gadget",     /* Flags, text                      */
  148.         &scrolldn_item } ;         /* Pointer to first menu item       */
  149.  
  150. /****************************************************************/
  151. /* Main program                                                 */
  152. /****************************************************************/
  153.  
  154. main()
  155. {
  156.    struct MenuItem *ItemAddress();
  157.    ULONG Signals, MIClass, MICode, itemnum;        
  158.    APTR MIAddress;
  159.    int i,k=0, gad_pos, real_pos;
  160.  
  161.    if (!(IntuitionBase = (struct IntuitionBase *)
  162.     OpenLibrary("intuition.library", LIBRARY_VERSION)))
  163.    {
  164.       printf("Can't open the intuition library\n");
  165.       MyCleanup();
  166.       exit(FALSE);
  167.    }
  168.  
  169.    if (!(GfxBase = (struct GfxBase *)
  170.     OpenLibrary("graphics.library", LIBRARY_VERSION)))
  171.    {
  172.       printf("Can't open the graphics library\n");
  173.       MyCleanup();
  174.       exit(FALSE);
  175.    }
  176.  
  177.       if (!(ControlWindow = (struct Window *)OpenWindow(&NewControlWindow)))
  178.       {
  179.          printf("Couldn't open the control window.\n");
  180.          MyCleanup();
  181.          exit(FALSE);
  182.       }
  183.  
  184.    SetMenuStrip(ControlWindow, &gadget_menu);
  185.  
  186.    for (i=0; i<10; i++)
  187.  
  188.    for (;;) {  /* wait for a signal and process it */
  189.  
  190.       Signals = Wait(1 << ControlWindow->UserPort->mp_SigBit);
  191.  
  192.       if (Signals & (1<< ControlWindow->UserPort->mp_SigBit)) {
  193.          /* Process the Intuition message */
  194.          while (MyIntuiMessage=(struct IntuiMessage *)
  195.                       GetMsg(ControlWindow->UserPort)) 
  196.          {
  197.             /* Get all the needed info and reply to message */
  198.             MIClass = MyIntuiMessage->Class;
  199.             MICode = MyIntuiMessage->Code;
  200.             MIAddress = MyIntuiMessage->IAddress;
  201.             ReplyMsg(MyIntuiMessage);
  202.             /* Determine what the message was for */
  203.  
  204.             switch (MIClass) {
  205.  
  206.                case MENUPICK:
  207.  
  208.                 while (MICode != MENUNULL)
  209.                 {
  210.                   itemnum = ITEMNUM(MICode);
  211.                   switch(itemnum)
  212.                    {
  213.                         /* User chose ghost */
  214.                         case 2:
  215.  
  216.             /* Remove the gadget from Intuition's control */
  217.             /* before we change any values.               */
  218.             gad_pos = RemoveGList(ControlWindow,&StrGadget[4],1);
  219.  
  220.             /* Do a bitwise exclusive OR to toggle the flag.     */
  221.             /* I do not use the functions OnGadget and OffGadget */
  222.             /* (which would produce the same effect), because    */
  223.             /* they call RefreshGadgets. This routine refreshes  */
  224.             /* ALL gadgets from that gadget on, and can cause    */
  225.             /* a noticable flicker depending upon where the      */
  226.             /* gadget is located in the list.             */
  227.                         StrGadget[4].Flags ^= GADGDISABLED;
  228.  
  229.             /* Return the gadget to Intuition's control. */
  230.             real_pos = AddGList(ControlWindow, &StrGadget[4], 
  231.                     gad_pos, 1, NULL);
  232.  
  233.             /* Refresh the display of the gadget. */
  234.                         RefreshGList(&StrGadget[4], ControlWindow, NULL,1);
  235.                         break;
  236.  
  237.                         /* User chose scroll up */
  238.                         case 1:
  239.                         if (k==9) k = 0; else k++;
  240.  
  241.             /* Remove the gadgets from Intuition's control */
  242.             /* before we change any values.               */
  243.             gad_pos = RemoveGList(ControlWindow, &StrGadget[4], 5);
  244.  
  245.                         for (i=0; i<5; i++)
  246.                         {
  247.                         StringInfo[i].Buffer = item_string[(i+k)%10];
  248.                         StringInfo[i].BufferPos = 0;
  249.                         StringInfo[i].NumChars = 0;
  250.                         }
  251.  
  252.             /* Return the gadgets to Intuition's control. */
  253.             real_pos = AddGList(ControlWindow, &StrGadget[4], 
  254.                     gad_pos, 5, NULL);
  255.  
  256.             /* Refresh the display of the gadgets. */
  257.                         RefreshGList(&StrGadget[4], ControlWindow, NULL,5);
  258.                         break;
  259.  
  260.                         /* User chose scroll down */
  261.                         case 0:
  262.                         if (k==0) k = 9; else k--;
  263.  
  264.             /* Remove the gadgets from Intuition's control */
  265.             /* before we change any values.               */
  266.             gad_pos = RemoveGList(ControlWindow, &StrGadget[4], 5);
  267.  
  268.                         for (i=0; i<5; i++)
  269.                         {
  270.                         StringInfo[i].Buffer = item_string[(i+k)%10];
  271.                         StringInfo[i].BufferPos = 0;
  272.                         StringInfo[i].NumChars = 0;
  273.                         }
  274.  
  275.             /* Return the gadgets to Intuition's control. */
  276.             real_pos = AddGList(ControlWindow, &StrGadget[4], 
  277.                     gad_pos, 5, NULL);
  278.  
  279.             /* Refresh the display of the gadgets. */
  280.                         RefreshGList(&StrGadget[4], ControlWindow, NULL,5);
  281.                         break;
  282.                    } /* switch */
  283.  
  284.                 MICode = (ItemAddress(&gadget_menu,MICode))->NextSelect;
  285.  
  286.                 } /* while */
  287.                 break;
  288.  
  289.                case CLOSEWINDOW:       /* bye! */
  290.                 /* reply to any outstanding messages */
  291.                   while (MyIntuiMessage = (struct IntuiMessage *) GetMsg(
  292.                    ControlWindow->UserPort))
  293.                     ReplyMsg(MyIntuiMessage);
  294.                   MyCleanup();
  295.                   exit(TRUE);
  296.                   break;
  297.  
  298.                default:
  299.                   printf("Unhandled Message Received.\n");
  300.                   break;
  301.             }  /* switch */
  302.          } /* while */
  303.       } /* if */
  304.    }  /* for */
  305. }  /* main */
  306.  
  307. MyCleanup()
  308. {
  309.    if (ControlWindow) CloseWindow(ControlWindow);
  310.    if (GfxBase) CloseLibrary(GfxBase);
  311.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  312. }
  313.